package com.cqc.weelviewdemo01;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lljjcoder.citypickerview.widget.CityPicker;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private Button btn;
    private Button btn2;
    private CityPicker cityPicker;
    private String province;//省份
    private String city;//城市
    private String district; //区县（如果设定了两级联动，那么该项返回空）
    private String code;//邮编

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initViews();
    }

    private void initViews() {
        btn.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    private void findViews() {
        tv = (TextView) findViewById(R.id.tv);
        btn = (Button) findViewById(R.id.btn);
        btn2 = (Button) findViewById(R.id.btn2);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn:
                cityPicker = new CityPicker.Builder(MainActivity.this).textSize(16) //滚轮文字的大小
                        .title("城市选择") //标题，设置名称
                        .titleBackgroundColor("#234Dfa") //标题背景
                        .confirTextColor("#000000")
                        .cancelTextColor("#000000")
                        .province("山东省")
                        .city("烟台市")
                        .district("莱山区")
                        .textColor(Color.parseColor("#000000"))
                        .provinceCyclic(true)
                        .cityCyclic(false)
                        .districtCyclic(false)
                        .visibleItemsCount(7)
                        .itemPadding(10)
                        .build();
                cityPicker.show();
                cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        province = citySelected[0];
                        city = citySelected[1];
                        district = citySelected[2];
                        code = citySelected[3];
                        tv.setText(province + "\n" + city + "\n" + district + "\n邮编：" + code);
                    }
                });
                break;
            case R.id.btn2:
                cityPicker = new CityPicker.Builder(MainActivity.this).textSize(16) //滚轮文字的大小
                        .title("城市选择")
                        .titleBackgroundColor("#234Dfa")
                        .confirTextColor("#000000")
                        .cancelTextColor("#000000")
                        .onlyShowProvinceAndCity(true)//只有省+市
                        .province("山东省")
                        .city("烟台市")
                        .textColor(Color.parseColor("#000000"))
                        .provinceCyclic(false)
                        .cityCyclic(false)
                        .districtCyclic(false)
                        .visibleItemsCount(7)
                        .itemPadding(10)
                        .build();
                cityPicker.show();
                cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        province = citySelected[0];
                        city = citySelected[1];
                        code = citySelected[3];
                        tv.setText(province + "\n" + city + "\n邮编：" + code);
                    }
                });
                break;
        }
    }

}
